#include "processbar.h"

#define NUM 104
#define SYMBOL ' '

const char *str = "-|\\/";

void process()
{
    char arr[NUM] = {0};
    arr[0] = SYMBOL;

    int cnt = 1;
    while(cnt <= 100)
    {
        printf("[%-100s][%3d%%][%c]\r",arr,cnt,str[cnt%4]);
        fflush(stdout);
        usleep(100000);
        arr[cnt++] = SYMBOL;
    }
    printf("\n");
}

#define DownLoadSize (1024*1024*1024) 

void download()
{
    srand((unsigned int)time(NULL));

    double _rate = 0;
    int total = DownLoadSize;

    while(total)
    {

        int speed = rand() % (1024 * 1024) * 10;
        total -= speed;

        if(total < 0)
        {
            total = 0;
        }

        _rate = (DownLoadSize - total) * 1.0  / DownLoadSize * 100;
        process_flush(_rate);
    }
    printf("\n");

}

char arr[NUM] = {0};
void process_flush(double rate)
{
    static int cnt = 0;

    int temp = (int)rate;
    if(temp < 100)
        arr[temp] = SYMBOL;

    //printf("[\033[0;35;47m%-100s\033[0m][%.1lf%%][%c]\r",arr,rate,str[cnt%4]);
    printf("[\033[0;47m%-100s\033[0m][%.1lf%%][%c]\r","",rate,str[cnt%4]);
    printf("[\033[0;45m%-s\033[0m\r",arr);

    fflush(stdout);
    usleep(100000);

    cnt++;
    cnt %= 4;
}


